package com.dd.cloud.user.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.constants.FeedbackConstants;
import com.dd.cloud.common.constants.UserConstants;
import com.dd.cloud.user.entity.admin.Feedback;
import com.dd.cloud.user.entity.admin.FeedbackReply;
import com.dd.cloud.user.entity.doctor.DoctorDetail;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.user.mapper.admin.FeedbackReplyMapper;
import com.dd.cloud.user.service.admin.IFeedbackReplyService;
import com.dd.cloud.user.service.admin.IFeedbackService;
import com.dd.cloud.user.service.doctor.IDoctorDetailService;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.common.enums.ZkExceptionEnum;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.utils.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 反馈的回复表 服务实现类
 * </p>
 *
 * @author czg
 * @since 2022-05-07
 */
@Service
public class FeedbackReplyServiceImpl extends ServiceImpl<FeedbackReplyMapper, FeedbackReply> implements IFeedbackReplyService {
    @Autowired
    private IFeedbackService feedbackService;
    @Autowired
    private IDoctorDetailService doctorDetailService;
    /**
     * 添加回复
     * @param reply
     */
    @Override
    public void addReply(FeedbackReply reply) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        Feedback feedback = feedbackService.getById(reply.getFeedbackId());
        if (feedback == null || feedback.getStatus()==FeedbackConstants.FeedbackStatus.DEL){
            throw new ZekeException(400,"反馈不存在");
        }
        if (loginInfo.getType() == LoginInfo.UserType.DOCTOR){
            QueryWrapper<DoctorDetail> qw = new QueryWrapper<>();
            qw.lambda().eq(DoctorDetail::getDoctorId, loginInfo.getId())
                    .eq(DoctorDetail::getStatus, UserConstants.DoctorDetailStatus.NORMAL);
            DoctorDetail doctorDetail = doctorDetailService.getOne(qw);
            if (doctorDetail != null){
                reply.setName(doctorDetail.getName());
            }
        }
        reply.setUserId(loginInfo.getId());
        reply.setUserType(loginInfo.getType());
        reply.setStatus(FeedbackConstants.replyStatus.NOMER);
        reply.setCreateDate(LocalDateTime.now());
        reply.setName(loginInfo.getName());
        boolean i = save(reply);
        if (!i){
            throw new ZekeException(ZkExceptionEnum.ADD_ERROR);
        }
    }

    /**
     * 查询回复列表
     * @param id 反馈的id
     * @return
     */
    @Override
    public List<FeedbackReply> getReplyPage(Integer id) {
        QueryWrapper<FeedbackReply> qw = new QueryWrapper<>();
        qw.lambda().eq(FeedbackReply::getFeedbackId, id);
        qw.lambda().ne(FeedbackReply::getStatus,FeedbackConstants.replyStatus.DEL);
        qw.lambda().orderByDesc(FeedbackReply::getCreateDate);
        List<FeedbackReply> list = list(qw);
        return list;
    }

    /**
     * 修改回复的状态
     * @param ids
     * @param status
     */
    @Override
    public void updateReplyStatus(String ids, Integer status) {
        String[] idStr = ids.split(",");
        if (!ObjectUtils.refrect(FeedbackConstants.replyStatus.class, status)){
            throw new ZekeException(ZkExceptionEnum.STATUS_DATA_ERROR);
        }
        UpdateWrapper<FeedbackReply> uw = new UpdateWrapper<>();
        uw.lambda().set(FeedbackReply::getStatus, status).in(FeedbackReply::getId, idStr);
        boolean i = update(uw);
        if (!i){
            throw new ZekeException(ZkExceptionEnum.UPDATE_ERROR);
        }
    }
}
